<?php

namespace Common\Job;
use WXAPI\Logic\WxsendLogic;
//  加载常量配置文件


class SendLiveMessageJob {
    public function setUp() {
        $data = $this->args;
        // ... Set up environment for this job
        fwrite(STDOUT, $data['type'] . '开始执行'.date('Y-m-d H:i:s') . PHP_EOL);
    }

    public function perform() {
        $data = $this->args;

        fwrite(STDOUT, $data['type'] . '(' . date('Y-m-d H:i:s', time()) . ')正在执行' . PHP_EOL);
        tag('action_begin');
        $time = time();
        $liveInfo = M('live')->where("live_status = 0 and start_time > $time")->select();

        $Wxsend = new WxsendLogic();
        foreach ($liveInfo as $k=>$v){

            if($v['start_time'] - $time >= 0){

                //把直播的预约人员拿出来发送订阅提醒
                $liveReservation = M('live_reservation r')
                    ->field('r.*,u.open_id,l.name,l.start_time,l.anchor_name')
                    ->join('ty_users u on u.user_id = r.member_id','left')
                    ->join('ty_live l on l.id = r.live_id','left')
                    // ->where(['r.live_id'=>16,'r.member_id'=>1001439])
                    ->where(['r.live_id'=>$v['id'],'r.wx_push'=>0])
                    ->select();

                foreach ($liveReservation as $key=>$val){
                    $sendData = [];
                    $sendData['live_title'] = date('Y-m-d').'直播！';
                    $sendData['live_name'] = $val['name'];
                    $sendData['start_time'] = date('Y-m-d H:i:s',$val['start_time']);
                    $sendData['live_member'] = $val['anchor_name'];
                    $sendData['live_content'] = $val['name'];
                    $sendData['live_id'] = $val['live_id'];
                    $b = M('live_reservation')->where(['member_id'=>$val['member_id'],'live_id'=>$val['live_id']])->save(['wx_push'=>1]);
                    $push = $Wxsend->sendSubscribeMessage($val['open_id'], 9, '3Z6vQs_Hj4vnbJq1o0rBUVOkfO1WfEDTfKEpI7_7ndo', $sendData);
                   
                }
                    
                
            }

        }
        //发送通知代码

    }

    public function tearDown() {
        $data = $this->args;
        // ... Set up environment for this job
        fwrite(STDOUT, $data['type'] . '结束执行'.date('Y-m-d H:i:s') . PHP_EOL);
    }
}
